package com.hongdee.crm.cartag.service.Impl;

import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.cartag.entity.TCrmSurveryQuestion;
import com.hongdee.crm.cartag.entity.TCrmSurveryQuestionResource;
import com.hongdee.crm.cartag.service.ITCrmCartagService;
import com.hongdee.crm.tag.dao.ITCrmCarTagDao;
import com.hongdee.crm.wo.entity.TCrmWoType;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 侯念晨 on 2017-05-12.
 */
@Service
public class ITCrmCartagServiceImpl implements ITCrmCartagService {
    @Autowired
    private ITCrmCarTagDao itCrmCarTagDao;
    /**
     * 分页查询
     */
    @Override
    public Map newPage(String value,String start, String limit, String uid) throws Exception{
        if(value.equals("")){
            String sql="select * from T_CRM_SURVERY_QUESTION";
        }
        String sql="select * from T_CRM_SURVERY_QUESTION where quesiton_name like ? or quesiton_code like  ?";
        List<Object> params = new ArrayList();
        /**
         * 把公司ID加入params中
         */
        params.add("%"+value+"%");
        params.add("%"+value+"%");
        /**
         * 返回
         */
        return this.itCrmCarTagDao.page(sql,params.toArray(),start,limit);

    }

    /*
 * 查询类别详情
 *
 * 请求参数: uid  token  id
 *
* */
    @Override
    public Map info(String id) throws Exception {
        String sql = "select * from T_CRM_SURVERY_QUESTION where id = ? ";
        return this.itCrmCarTagDao.info(sql, id);
    }

    /*
     * 启用停用
    *
     * 请求参数:uid    登录用户 ID
               token  登录用户 token
                id     记录主键

       返回值: success:请求状态，true|false
               msg: 请求结果信息，
    * */
    @Override
    public void update(String id, String status) throws Exception {
        if(null==id||id.equals("")){
            throw new Exception("请选择记录");
        }
        String hql = "update TCrmSurveryQuestion set status = :status where id = :id ";
        Map params = new HashMap();
        params.put("id", id);
        params.put("status", status);
        this.itCrmCarTagDao.update(hql, params);
    }

    /*
    *    保存工单对应的类别
    *
    *    请求参数  : uid    登录用户 ID
                     token  登录用户 token
                     data  TCrmWoType实体的信息

           返回值 : success:请求状态，true|false
                    msg: 请求结果信息
    * */
    @Override
    public TCrmSurveryQuestion saveOrUpdate(TCrmSurveryQuestion tCrmSurveryQuestion, String uid) throws Exception {
        this.itCrmCarTagDao.saveOrUpdate(tCrmSurveryQuestion);
        return  tCrmSurveryQuestion;
    }
    /**
     * 查询左侧资源数
     * 发布方法名称BuildTreeChild
     */
//    @Override
//    public List tree(String id) throws Exception {
//        if (StringUtils.isNullOrEmpty(id)) {
//            id = "0";
//        }
//        String sqlStr = "select * from t_crm_survery_item where group_code = ? order by disp_order asc";
//        Object[] params = new Object[]{id};
//        List<Map<String, Object>> treeP = this.itCrmCarTagDao.list(sqlStr, params);
//        return buildTreeParent(treeP);
//    }

    /**
     * 方法名称: BuildTreeParent
     * 方法描述: 获取当前节点的子节点，并生成树状结点，结合BuildTreeChild使用
     * 参数名称: List<Map<String, Object>> treeP 树节点集合
     * 返回内容: 无
     * 异常说明: 抛出异常Exception
     * 创建用户: MissJin
     * 创建时间: 2017/1/21/16:11
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
//    private List buildTreeParent(List<Map<String, Object>> treeP) throws Exception {
//
//        if (treeP.size() == 0) {
//            return treeP;
//        }
//
//        for (Map map : treeP) {
//            String resId = String.valueOf(map.get("id"));
//            List<Map<String, Object>> childList = tree(resId);
//            map.put("child", childList);
//        }
//        return treeP;
//    }
    /**
     * 查询Group表数据
     */
    private List AllGroup() throws Exception{
        return this.itCrmCarTagDao.list("select * from T_Crm_survery_group");
    }
    /**
     * 查询item表
     */
    private List item(String groupCode) throws Exception{
        return this.itCrmCarTagDao.list("select *from T_Crm_survery_item where group_code = ?",groupCode);
    }
    /**
     * 拼接树结构
     */
    @Override
    public List tree() throws Exception {
        List<Map> group = AllGroup();
        List<Map> all =new ArrayList<>();

        for (Map temp : group){
            Map mapGroup = new HashMap();
            List<Map> item = new ArrayList<>();
            //item
            List<Map> tempitem = item(String.valueOf(temp.get("id")));
            for(Map tempS:tempitem){
                Map mapitem = new HashMap();
                mapitem.put("label",tempS.get("itemName"));
                mapitem.put("value",tempS.get("itemCode"));
                item.add(mapitem);
            }
            //group
            mapGroup.put("label",temp.get("groupName"));
            mapGroup.put("value",temp.get("groupCode"));
            mapGroup.put("child",item);
            all.add(mapGroup);
        }
        return all;
    }
    /***
     *
     * @param questionId 问题主键
     * @param itemIds 分配的资源主键数据
     * @throws Exception
     */
    @Override
    public List saveItemTree(String questionId, String[] itemIds) throws Exception {
        // 先删除
        // 后保存
        String delHQL = " delete from T_Crm_Survery_Question_Resource where question_id = ? ";
//        Map<String, Object> params = new HashMap();
//        params.put("questionId", questionId);
        this.itCrmCarTagDao.executeSQL(delHQL, questionId);
        List entityList = new ArrayList();
        TCrmSurveryQuestionResource roleResource;
        for (String itemId : itemIds) {

            List param = new ArrayList();
            String sql="select G.group_code from T_Crm_survery_item T , T_Crm_survery_group G where T.item_code = ? "
                    +"and T.group_code = G.id";
            param.add(itemId);
            Map groupcode = this.itCrmCarTagDao.info(sql,param.toArray());
            roleResource = new TCrmSurveryQuestionResource();
            roleResource.setItemCode(itemId);
            roleResource.setQuestionId(questionId);
            roleResource.setGroupCode(String.valueOf(groupcode.get("groupCode")));
            entityList.add(roleResource);
        }
        this.itCrmCarTagDao.saveOrUpdateList(entityList);
        return this.itCrmCarTagDao.list("select * from t_crm_survery_group where group_code in ( " +
                "select group_code from T_Crm_Survery_Question_Resource where question_id=? )",questionId);
    }
    /**
     * groupPage查询
     * @param questionId 问题id
     * @param groupCode
     */
    @Override
    public Map groupPage(String questionId , String groupCode, String start, String limit) throws Exception{
        String sql="SELECT * FROM t_crm_survery_item  T WHERE " +
                "T.item_code IN(SELECT R.item_code FROM t_crm_survery_question_resource R WHERE" +
                " R.question_id=? AND R.group_code = ?)";
        List<Object> params = new ArrayList();
        params.add(questionId);
        params.add(groupCode);
        return this.itCrmCarTagDao.page(sql,params.toArray(),start,limit);
    }
    /**
     * 评分项查询
     */
    @Override
    public Map itempage(String itemId,String start, String limit) throws Exception{
        String sql ="select * from t_crm_survery_item_area where item_code = ?";
        List<Object> params = new ArrayList();
        params.add(itemId);
        return this.itCrmCarTagDao.page(sql,params.toArray(),start,limit);
    }
}
